package com.hsnn.medstgmini.settlement.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.settlement.model.SetBankAcctInfo;
import com.hsnn.medstgmini.settlement.model.SetBankArea;
import com.hsnn.medstgmini.settlement.model.SetBankInfo;
import com.hsnn.medstgmini.settlement.service.BankAcctInfoManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.ImgUtil;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 帐户信息
 * @author 蔡春龙
 * @date 2015年7月21日
 */
@Controller
@RequestMapping("/acctInfo")
public class AcctInfoController {

	private static final Logger log = Logger
			.getLogger(AcctInfoController.class);

	@Autowired
	private BankAcctInfoManager bankAcctInfoManager;

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private HttpSession session;

	/**
	 * @category 获取当前登陆人信息
	 * @date 2015年6月14日
	 * @return
	 */
	public SysUser getUser() {
		return (SysUser) session.getAttribute(Constants.USERINFO);
	}

	/**
	 * @category 跳转支行列表页面
	 * @date 2015年7月21日
	 * @param model
	 * @param bankAcctNo
	 * @return
	 */
	@RequestMapping("/toSubBankList")
	public String toSubBankList(Model model, String bankAcctNo) {
		model.addAttribute("bankAcctNo", bankAcctNo);
		return "/settle/bankAcctInfoSubbranch";
	}

	/**
	 * @category 获取支行列表
	 * @date 2015年7月21日
	 * @param address
	 * @return
	 */
	@RequestMapping("/getSubBankList")
	@ResponseBody
	public Pagination getSubBankList(SetBankInfo bankInfo) {
		log.info("enter getSubBankList method");
		Pagination page = new Pagination(request);
		try {
			bankAcctInfoManager.getSubBankAcctInfoList(page, bankInfo);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}

	/**
	 * @category 查询银行地区列表
	 * @date 2015年7月22日
	 * @param bankArea
	 * @return
	 */
	@RequestMapping("/getBankAreaList")
	@ResponseBody
	public Pagination getBankAreaList(SetBankArea bankArea) {
		log.info("enter getBankAreaList method");
		Pagination page = new Pagination(request);
		try {
			List<SetBankArea> list = bankAcctInfoManager
					.getBankAreaList(bankArea);
			page.setRows(list);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}

	/**
	 * @category 保存银行帐户信息
	 * @date 2015年7月22日
	 * @param bankAcct
	 * @return
	 */
	@RequestMapping("/saveBankAcctInfo")
	@ResponseBody
	public Pagination saveBankAcctInfo(SetBankAcctInfo bankAcct,
			MultipartFile licence) {
		log.info("enter saveBankAcctInfo method");
		Pagination page = new Pagination(request);
		FileOutputStream fos = null;
		try {
			String bankAcctId = bankAcct.getBankAcctId();
			if (StringUtils.isBlank(bankAcctId)) { // 新增银行卡需要上传开户许可证
				if (licence == null || licence.isEmpty()) {
					page.setSuccess(false);
					page.setMsg("请上传开户许可证！");
					return page;
				}
			}
			if (licence != null) {
				if (licence.getSize() > 3 * 1024 * 1024) {
					page.setSuccess(false);
					page.setMsg("开户许可证不能大于3M！");
					return page;
				}
				String name = licence.getOriginalFilename();
				if (!ImgUtil.isImg(name)) {
					page.setSuccess(false);
					page.setMsg("开户许可证请选择图片，图片支持格式：gif, jpg, jpeg, png, bmp！");
					return page;
				}
				String path = session.getServletContext().getRealPath("/");
				path = path.replaceAll("\\\\", "/") + "upload/licence/";
				String filename = path + name;
				File file = new File(filename);
				fos = new FileOutputStream(file);
				fos.write(licence.getBytes());
				fos.flush();
				bankAcct.setAcctApprovalPicDir("upload/licence/" + name);
			}
			bankAcct.setAuditUser(null);
			bankAcct.setAuditTime(null);
			bankAcct.setAuditRemark(null);
			bankAcctInfoManager.saveBankAcctInfo(bankAcct, getUser());
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return page;
	}

	/**
	 * @category 保存银行帐户审核信息
	 * @date 2015年7月23日
	 * @param bankAcct
	 * @param licence
	 * @return
	 */
	@RequestMapping("/saveAuditBankAcctInfo")
	@ResponseBody
	public Pagination saveAuditBankAcctInfo(SetBankAcctInfo bankAcct,
			MultipartFile licence) {
		log.info("enter saveBankAcctInfo method");
		Pagination page = new Pagination(request);
		FileOutputStream fos = null;
		try {
			String bankAcctId = bankAcct.getBankAcctId();
			if (StringUtils.isBlank(bankAcctId)) { // 新增银行卡需要上传开户许可证
				if (licence == null || licence.isEmpty()) {
					page.setSuccess(false);
					page.setMsg("请上传开户许可证！");
					return page;
				}
			}
			if (licence != null) {
				if (licence.getSize() > 3 * 1024 * 1024) {
					page.setSuccess(false);
					page.setMsg("开户许可证不能大于3M！");
					return page;
				}
				String name = licence.getOriginalFilename();
				if (!ImgUtil.isImg(name)) {
					page.setSuccess(false);
					page.setMsg("开户许可证请选择图片，图片支持格式：gif, jpg, jpeg, png, bmp！");
					return page;
				}
				String path = session.getServletContext().getRealPath("/");
				path = path.replaceAll("\\\\", "/") + "upload/licence/";
				String filename = path + name;
				File file = new File(filename);
				fos = new FileOutputStream(file);
				fos.write(licence.getBytes());
				fos.flush();
				bankAcct.setAcctApprovalPicDir("upload/licence/" + name);
			}
			bankAcct.setAuditUser(getUser().getId());
			bankAcct.setAuditTime(new Date());
			bankAcctInfoManager.saveAuditBankAcctInfo(bankAcct, getUser());
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return page;
	}
	
	/**
	 * @category 中心、卫生局编辑银行帐户信息
	 * @date 2015年7月22日
	 * @param bankAcct
	 * @return
	 */
	@RequestMapping("/saveBankAcctInfoByCenter")
	@ResponseBody
	public Pagination saveBankAcctInfoByCenter(SetBankAcctInfo bankAcct,
			MultipartFile licence) {
		log.info("enter saveBankAcctInfoByCenter method");
		Pagination page = new Pagination(request);
		FileOutputStream fos = null;
		try {
			String bankAcctId = bankAcct.getBankAcctId();
			if (StringUtils.isBlank(bankAcctId)) { // 新增银行卡需要上传开户许可证
				if (licence == null || licence.isEmpty()) {
					page.setSuccess(false);
					page.setMsg("请上传开户许可证！");
					return page;
				}
			}
			if (licence != null) {
				if (licence.getSize() > 3 * 1024 * 1024) {
					page.setSuccess(false);
					page.setMsg("开户许可证不能大于3M！");
					return page;
				}
				String name = licence.getOriginalFilename();
				if (!ImgUtil.isImg(name)) {
					page.setSuccess(false);
					page.setMsg("开户许可证请选择图片，图片支持格式：gif, jpg, jpeg, png, bmp！");
					return page;
				}
				String path = session.getServletContext().getRealPath("/");
				path = path.replaceAll("\\\\", "/") + "upload/licence/";
				String filename = path + name;
				File file = new File(filename);
				fos = new FileOutputStream(file);
				fos.write(licence.getBytes());
				fos.flush();
				bankAcct.setAcctApprovalPicDir("upload/licence/" + name);
			}
			bankAcct.setAuditUser(null);
			bankAcct.setAuditTime(null);
			bankAcct.setAuditRemark(null);
			bankAcctInfoManager.saveBankAcctInfoByCenter(bankAcct, getUser());
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return page;
	}

	/**
	 * @category 图片展示
	 * @date 2015年7月22日
	 * @param model
	 * @param imgPath
	 * @return
	 */
	@RequestMapping("/toImg")
	public String toImg(Model model) {
		return "/settle/img";
	}
	
	/**
	 * @category 校验银行卡号是否存在
	 * @date 2015年8月11日
	 * @param bankAcct
	 * @return
	 */
	@RequestMapping("/checkSameBankAcctNo")
	@ResponseBody
	public Pagination checkSameBankAcctNo(SetBankAcctInfo bankAcct) {
		log.info("enter checkSameBankAcctNo method");
		Pagination page = new Pagination(request);
		boolean exists = bankAcctInfoManager.checkSameBankAcctNo(bankAcct);
		page.setSuccess(exists);
		return page;
	}

}
